//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO;
using System.IO.Packaging;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing.Media
{
    sealed class EmbeddedWavAudioFile : MediaObject, IOfficeXmlSerializableAs
    {
        private string _name;
        private bool builtIn;
        private byte[] data;
        internal const string xmlName = "wavAudioFile";
        public override void ReadXml(ReadContext context)
        {
            using(var c = context.Read(xmlName))
            {
                string embed = c.GetString(OfficeConstants.RelationshipNamespace + "embed");
                var relationship = c.Part.GetRelationship(embed);
                var uri = PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri);
                var part = c.Part.Package.GetPart(uri);
                using (Stream stream = part.GetStream())
                {
                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                }
                this._name = c.GetOptionalString("name");
                this.builtIn = c.GetOptionalBoolean("builtIn", false);
            }
        }

        public override void WriteXml(WriteContext context)
        {
            using (var c = context.Write(xmlName))
            {
                c.SetOptionalString("name", _name );
                c.SetOptionalBoolean("builtIn", false, builtIn );
                var part = context.Part.Package.CreatePart(c.GetAvailableUri("/media/wav{0}.wav"),"audio/wav");
                using (Stream stream = part.GetStream())
                {
                    stream.Write(data, 0, data.Length);
                }
                var relationship = c.Part.CreateRelationship(part.Uri, TargetMode.Internal, DrawingConstants.audioRelationship);
                c.SetString(OfficeConstants.RelationshipNamespace + "embed", relationship.Id);

            }
        }

        public void ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                string embed = c.GetString(OfficeConstants.RelationshipNamespace + "embed");
                var relationship = c.Part.GetRelationship(embed);
                var uri = PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri);
                var part = c.Part.Package.GetPart(uri);
                using (Stream stream = part.GetStream())
                {
                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                }
                this._name = c.GetOptionalString("name");
                this.builtIn = c.GetOptionalBoolean("builtIn", false);
            }
        }

        public void WriteXmlAs(string name, WriteContext context)
        {
            using (var c = context.Write(name))
            {
                c.SetOptionalString("name", this._name);
                c.SetOptionalBoolean("builtIn", false, builtIn);
                var part = context.Part.Package.CreatePart(c.GetAvailableUri("/media/wav{0}.wav"), "audio/wav");
                using (Stream stream = part.GetStream())
                {
                    stream.Write(data, 0, data.Length);
                }
                var relationship = c.Part.CreateRelationship(part.Uri, TargetMode.Internal, DrawingConstants.audioRelationship);
                c.SetString(OfficeConstants.RelationshipNamespace + "embed", relationship.Id);

            }
        }
    }
}